<?php

namespace libs;

class view {
    
    public $_cache;
    public $_cachetime;
    public $_cacheoptions;
    private $_currentpage;
    
    public function __construct($cacheoptions = false) 
    {
        $this->_cacheoptions = $cacheoptions;
    }
    
    function render($class, $method, $header = 'header.php', $footer = 'footer.php') 
    {
        
        $folder = preg_replace('/Model/','',$class);
        $folder = preg_replace('/models/','',$folder);
        
        $file = ('views'. DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $method . '.php');
        
        if($header) include_once 'views/'.$header;
        if(file_exists($file)) 
        {
            include $file;
        }
        if($footer) include_once 'views/'.$footer;
        
         
    }
    
    function renderTwig($class,$method,$templateview = array(),$layout_with_dbconnection = false)
    {
        $folder = preg_replace('/_controller/','',$class);
        $folder = preg_replace('/controllers/','',$folder);
        
        $loader = new \Twig_Loader_Filesystem('views/');
        $twig = new \Twig_Environment($loader);
        
        if($layout_with_dbconnection == true)
        {
            $function = new \Twig_SimpleFunction('renderLayout', function() {
                $layout = new \controllers\layout_controller(array('dbconfig'=>'config.ini'));
                return $layout->layout();
            });
            
            $twig->addFunction($function);
        }
        
        $request = explode('=',$_SERVER['QUERY_STRING']);
        
        @$main = explode('/',$request[1]);
        
        if(empty($main[0]))
        {
            $main[0] = 'glowna';
        }
        
        $twig->addGlobal("CurrentUrl", $main[0]);
        $twig->addGlobal('post', $_POST);
        $twig->addGlobal('URL', URL);
        $twig->addGlobal('UPLOAD_PATH', UPLOAD_PATH);
        
        $templatename = $folder.'/'.$method.'.html.twig';
        
        $template = $twig->loadTemplate($templatename);
       
        
        if(is_array($this->_cacheoptions))
        {
            foreach($this->_cacheoptions as $pages)
            {
                if($pages['page'] == $templatename)
                {
                    $this->_currentpage = $pages['page'];
                    $this->_cachetime = $pages['cachetime'];
                    $this->cacheCheck($template,$templateview);
                    return true;
                }
            }
        }
        elseif(is_string($this->_cacheoptions))
        {
            $this->_currentpage = $templatename;
            $this->_cachetime = 30;
            $this->cacheCheck($template,$templateview);
            return true;
        }
        
        echo $template->render($templateview);
        
    }
    
    
    
    public function cacheCheck(\Twig_Template $template,$templateview)
    {
        $file = 'cache'.$this->_currentpage;
        
        @$seconds = time() - filemtime($file);
        
        if(file_exists($file) && $seconds < $this->_cachetime)
        {
            include $file;
        }
        else
        {
            ob_start();
        
            echo $template->render($templateview);

            $template = ob_get_contents();

            ob_end_clean();
        
            file_put_contents('cache'.$this->_currentpage, $template);
            
            include $file;
        }
    }
    
    function renderAdminTwig($class,$method,$array = array())
    {
        $folder = preg_replace('/_controller/','',$class);
        $folder = preg_replace('/controllers/','',$folder);
        
        $folder = preg_replace('/admin\\\/','',$folder);
        
        $loader = new \Twig_Loader_Filesystem('views/admin');
        $twig = new \Twig_Environment($loader, array('debug' =>true));
        
        $twig->addExtension(new \Twig_Extension_Debug());
        
        $request = explode('_SkyCMS/',$_GET['url']);
        
        @$main = explode('/',$request[1]);
        
        $main[0] = '/'.$main[0];
        
        $twig->addGlobal("CurrentUrl", $main[0]);
        $twig->addGlobal('session', $_SESSION);
        $twig->addGlobal('server', $_SERVER);
        $twig->addGlobal('URL', URL);
        $twig->addGlobal('UPLOAD_PATH', UPLOAD_PATH);
        
        $template = $twig->loadTemplate($folder.'/'.$method.'.html.twig');
        
        echo $template->render($array);
        
    }
    
    
    
   
}

?>
